import { observable, action } from 'mobx-miniprogram'
import { getActivatePluginParam, checkWxCardState, getBaseInfo } from "@src/api/member";
import $cache from "@src/common/wxCache";
import router from "@src/router";

export default observable({
	// 数据字段
	isMember: null,
	memberName: '',
	name: '',
	phone: '',
	wxName: '',
	wxHeadImg: '',
	categoryId: 0,
	cardNo: '',
	categoryName: '',
	categoryImage: '',
	discount: 0,
	expireTime: '',
	status: 1,
	sex: 0,
	xhOpenId: '',
	cardOrder: null,
	// 计算属性
	get sum() {
		return '';
	},
	// actions
	initBase: action(function() {
		return new Promise((resolve, reject) => {
			if (this.isMember === null) {
				this.getBaseInfo().then(res => {
					wx.nextTick(() => {
						resolve();
					})
				}).catch(reason => {
					resolve();
				});
			} else {
				resolve();
			}
		})
	}),
	getBaseInfo: action(function() {
		return getBaseInfo().then(res => {
			this.xhOpenId = (res.data && res.data.openId) || ''
			if (res.code === 0) {
				const memberInfo = res.data;
				$cache.set("base_info", memberInfo);
				this.isMember = true;
				this.categoryId = memberInfo.memberCategoryId
				this.categoryName = memberInfo.memberCategoryName
				this.categoryImage = memberInfo.memberCategoryImage
				this.discount = memberInfo.memberCategoryDiscount
				this.expireTime = memberInfo.expireTime
				this.status = memberInfo.status
				this.cardOrder = memberInfo.memberCategoryOrder
				this.cardNo = memberInfo.cardNo
				if (memberInfo.name) {
					this.memberName = memberInfo.name
				} else {
					this.memberName = memberInfo.wxName
				}
				this.name = this.memberName
				this.sex = memberInfo.sex
				this.phone = memberInfo.phone
				this.wxName = memberInfo.wxName
				this.wxHeadImg = memberInfo.wxHeadImg
			} else if (res.code === 50404) {
				this.isMember = false;
				this.categoryId = 0;
				this.cardNo = '';
				this.expireTime = null;
				this.cardOrder = null;
				const memberInfo = res.data;
				this.wxName = memberInfo.wxName
				this.name = memberInfo.wxName
				this.wxHeadImg = memberInfo.wxHeadImg
				this.phone = memberInfo.phone
			} else {
				this.isMember = false;
				this.categoryId = 0;
				this.cardNo = '';
				this.expireTime = null;
				this.cardOrder = null;
			}
		})
	}),
	syncWxCard: action(function(isNavMini = false) {
		wx.showLoading({
			title: "同步微信卡包...",
			mark: true
		});
		console.log(1)

		getActivatePluginParam().then(res => {
			console.log(2)
			wx.hideLoading();
			if (res.code === 0) { //请求成功
				console.log(3)
				let cardData = {
					encrypt_card_id: res.data.encryptCardId,
					outer_str: res.data.outerStr,
					biz: res.data.biz
				};
				wx.navigateToMiniProgram({
					appId: 'wxeb490c6f9b154ef9', //固定为此 appid，不可改动
					extraData: cardData, // 包括 encrypt_card_id, outer_str, biz三个字段，须从 step3 中获得的链接中获取参数
					success: function() {
						console.log("微信调用成功");
					},
					fail: function() {
						wx.showToast({
							title: "调用开卡组件失败",
							icon: "none",
							duration: 1500
						})
					},
					complete: function() {
						wx.navigateBack();
					}
				});
			} else if (res.code === 65001) {
				console.log(4)
				let miniProgram = getApp().globalData.isNavMini
				const appOps = wx.getEnterOptionsSync()
				if (miniProgram && appOps.scene === 1037) {
					console.log(5)
					wx.showModal({
						title: '提示',
						showCancel: false,
						content: '您已同步过卡包',
						success: () => {
							const appOps = wx.getEnterOptionsSync()
							if (appOps.scene === 1037) {
								console.log(6)
								wx.exitMiniProgram()
							} else {
								console.log(7)
								router.push({
									name: 'index'
								})
							}
						}
					})
					return
				}
				console.log(8)
				wx.showModal({
					title: '提示',
					content: '您已同步过卡包',
					success: async (sm) => {
						if (sm.confirm) {
							checkWxCardState()
								.then(r => {
									wx.openCard({
										cardList: [{
											cardId: r.data
												.cardId,
											code: r.data
												.userCardCode
										}],
										success: () => {
											getApp().globalData
												.isNavCard = true
										}
									});
								})
							// 判断来自收银台开卡
							// let miniProgram = $cache.get('miniProgram')
							// console.log('判断来自收银台开卡-miniProgram',miniProgram)
							// // 1037 小程序跳转
							// if(miniProgram && appOps.scene === 1037) {
							//   wx.exitMiniProgram({
							//     fail: (err) => {
							//       console.log('跳转关闭失败')
							//       console.log(err)
							//       router.push({
							//         name: 'index'
							//       })
							//     }
							//   })
							// } else {
							//   router.push({
							//     name: 'mine'
							//   })
							// }


							// if(miniProgram){
							//   wx.exitMiniProgram()
							//   // wx.navigateBackMiniProgram({
							//   //   extraData: {
							//   //     miniProgram:miniProgram
							//   //   },
							//   //   // 退出小程序自动销毁
							//   //   // success(res) {
							//   //   //   // 返回成功
							//   //   //   $cache.del('miniProgram')
							//   //   // }
							//   // })
							// }else{
							//   console.log('首页')
							//   router.push({
							//     name: 'index'
							//   })
							// }
						} else if (sm.cancel) {}
					}
				})
			} else {
				console.log(9)
				console.error("获取激活信息识别", res)
			}
		}).catch((error) => {
			console.log(10)
			console.error("获取激活信息识别", error)
			wx.hideLoading();
		})
	})
})
